package com.rongshu.view.userInfo.activity;

import android.content.Intent;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.rongshu.R;
import com.rongshu.utils.Common;
import com.rongshu.utils.Constant;
import com.rongshu.utils.RxHelper;
import com.rongshu.view.base.BaseActivity;
import com.rongshu.view.login.entity.VerificationCode;
import com.rongshu.view.userInfo.iview.SafetyVerificationView;
import com.rongshu.view.userInfo.presenter.SafetyVerificationPresenter;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import rx.Subscriber;

public class SafetyVerificationCompareActivity extends BaseActivity<SafetyVerificationPresenter> implements SafetyVerificationView {
    @BindView(R.id.layout_close)
    LinearLayout layout_close;
    @BindView(R.id.layout_back)
    LinearLayout layout_back;
    @BindView(R.id.txt_top_title)
    TextView txt_top_title;
    @BindView(R.id.txtGetVerification)
    TextView txtGetVerification;
    @BindView(R.id.edtVerification)
    TextView edtVerification;
    @BindView(R.id.btn_confirm)
    Button btn_confirm;

    //intent
    private Intent intent = null;
    //phone
    private String phone = null;
    //map
    Map<String, Object> map = null;
    //倒计时时间
    private int sumTimes = 60;
    //code
    private String smsCode = null;
    //boolean
    private boolean is_input_code = false;

    @Override
    protected int initLayout() {
        return R.layout.activity_safety_verification_compare_view;
    }

    @Override
    protected void initView() {
        //set presenter
        setPresenter(new SafetyVerificationPresenter(this), this);
        //getIntent
        intent = getIntent();
        if (intent != null) {
            phone = intent.getStringExtra(Constant.INTENT_DATA);
        }
        //top
        layout_back.setVisibility(View.GONE);
        layout_close.setVisibility(View.VISIBLE);
        txt_top_title.setText("安全验证");
    }

    @Override
    protected void initData() {
        edtVerification.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() > 0) {
                    is_input_code = true;
                } else {
                    is_input_code = false;
                }

                btn_confirm.setBackgroundResource((is_input_code) ?
                        R.drawable.rectangle_three_green : R.drawable.rectangle_three_white);
                btn_confirm.setTextColor((is_input_code) ?
                        getResources().getColor(R.color.white) : getResources().getColor(R.color.color_4a4a4a));
            }
        });
    }

    @Override
    protected void updateViews(boolean isRefresh) {
        getPhoneVerficationCode();
    }

    /**
     * 获取验证码
     */
    private void getPhoneVerficationCode() {
        map = new HashMap<>();
        map.put("mobile", phone);
        map.put("type", 1);//1 APP验证码登录，2 APP忘记密码-重置密码，3 APP手机绑定，4 APP更换手机号码
        presenter.getPhoneVerficationCode(
                (String) map.get("mobile"),
                (int) map.get("type"),
                Common.encryptionSign(map));
        map.clear();
    }

    /**
     * 倒计时
     */
    private void countdown() {
        //倒计时操作
        RxHelper.countdown(sumTimes)
                .subscribe(new Subscriber<Integer>() {
                    @Override
                    public void onCompleted() {
                        txtGetVerification.setText(R.string.txt_get_verification_string);
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(Integer integer) {
                        txtGetVerification.setText(integer + "s");
                    }
                });
    }


    @OnClick({R.id.layout_close, R.id.txtGetVerification, R.id.btn_confirm})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.layout_close:
                showKeyboard(false);
                finish();
                break;
            case R.id.txtGetVerification://验证码
                getPhoneVerficationCode();
                break;
            case R.id.btn_confirm:
                String code = edtVerification.getText().toString().trim();
                if (TextUtils.isEmpty(code)) {
                    toast(getString(R.string.edt_verification_string));
                    return;
                }

                if (!code.equals(smsCode)) {
                    toast(getString(R.string.the_verification_code_does_not_match));
                    return;
                }
                showKeyboard(false);
                setResult(RESULT_OK);
                finish();
                break;
        }
    }

    /**
     * 获取验证码回调
     *
     * @param verificationCode
     */
    @Override
    public void getVerificationResult(VerificationCode verificationCode) {
        if (verificationCode.getErrCode() == 0) {
            VerificationCode.ResponseBean responseBean = verificationCode.getResponse();
            if (responseBean != null) {
                //倒计时
                countdown();
                //记录smsCode
                this.smsCode = responseBean.getSMSCode();
            }
        } else {
            toast(verificationCode.getErrMsg());
        }
    }

    @Override
    public void onBackPressed() {
        showKeyboard(false);
        super.onBackPressed();
    }
}
